package com.cn.ccp.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class JNIUtil {

	static{
		try {
			System.loadLibrary("msvcr120");
			System.loadLibrary("msvcp120");
//			System.loadLibrary("vccorlib120");
			System.loadLibrary("htemckcrypt");
//			System.load(new File(CCPContants.REGIST_PATH + "vccorlib120.dll").getCanonicalPath());
//			System.load(new File(CCPContants.REGIST_PATH + "msvcr120.dll").getCanonicalPath());
//			System.load(new File(CCPContants.REGIST_PATH + "msvcp120.dll").getCanonicalPath());
//			System.load(new File(CCPContants.REGIST_PATH + "htemckcrypt.dll").getCanonicalPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 
	public native byte[] encode(byte[] buf);
	
	public native byte[] decode(byte[] buf);
	
	
	/**
	08
	   *
	09
	   * @param in The InputStream to read the bytes from.
	10
	   * @return A byte array containing the bytes that were read.
	11
	   * @throws IOException If I/O error occurred.
	12
	   */
	  public  final byte[] readFully(InputStream in)
	    throws IOException
	  {
	    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
	    transfer(in, out);
	    out.close();
	    return out.toByteArray();
	  }
	 
	  /**
	24
	   * Transfers all bytes that can be read from <tt>in</tt> to <tt>out</tt>.
	25
	   *
	26
	   * @param in The InputStream to read data from.
	27
	   * @param out The OutputStream to write data to.
	28
	   * @return The total number of bytes transfered.
	29
	   */
	  public final long transfer(InputStream in, OutputStream out)
	    throws IOException
	  {
	    long totalBytes = 0;
	    int bytesInBuf = 0;
	    byte[] buf = new byte[4096];
	    while ((bytesInBuf = in.read(buf)) != -1) {
	      out.write(buf, 0, bytesInBuf);
	      totalBytes += bytesInBuf;
	    }
	    return totalBytes;
	  }
	
	public static void main(String[] args){
		JNIUtil jni = new JNIUtil();
	}
	
	
}
